package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.UserAddressMapper;
import com.sky.service.UserAddressService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class UserAddressServiceImpl implements UserAddressService {

    private final UserAddressMapper userAddressMapper;

    @Override
    public void addAddress(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        userAddressMapper.addAddress(addressBook);
    }

    @Override
    public List<AddressBook> selectAllData() {
        List<AddressBook> addressBookList = userAddressMapper.selectAllData();
        return addressBookList;
    }

    @Override
    public List<AddressBook> selectDefaultData() {
        List<AddressBook> addressBookDefaultList = userAddressMapper.selectDefaultData();
        return addressBookDefaultList;
    }

    @Override
    public void updateAddress(AddressBook addressBook) {
        userAddressMapper.updateAddress(addressBook);
    }

    @Override
    public AddressBook selectAddressInfoById(Long id) {
        AddressBook addressBook = userAddressMapper.selectAddressInfoById(id);
        return addressBook;
    }

    @Override
    public void deleteAddressById(Long id) {
        userAddressMapper.deleteAddressById(id);
    }

    @Transactional
    @Override
    public void setDefaultAddress(AddressBook addressBook) {
        userAddressMapper.updateAddressNODefault();
        userAddressMapper.updateAddressMyDefault(addressBook.getId());
    }

}
